package net.lainiao.config;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.util.logging.Logger;

/**
 * Created by Administrator on 2016/10/26.
 */
@Configuration
public class MyBatisConfig implements ApplicationContextAware {
    private Logger logger=Logger.getLogger(this.getClass().getName());
    private static ApplicationContext context;

    @Bean
    public DataSource dataSource(){
        logger.info("创建DataSource成功");
        BasicDataSource basicDataSource=new BasicDataSource();
        basicDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        basicDataSource.setUrl("jdbc:mysql://127.0.0.1:3306/shishuocms?useUnicode=true&characterEncoding=UTF-8");
        basicDataSource.setUsername("root");
        basicDataSource.setPassword("root");
        return basicDataSource;
    }

    @Bean(name="sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        logger.info("创建sqlSessionFactory成功");
        SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean();
        DataSource dataSource= (DataSource) context.getBean("dataSource");
        sqlSessionFactoryBean.setDataSource(dataSource);
        PathMatchingResourcePatternResolver  pathMatchingResourcePatternResolver=new PathMatchingResourcePatternResolver();
        Resource[] resources=pathMatchingResourcePatternResolver.getResources("classpath:mapper/*.xml");
        logger.info("resources 数量:"+resources.length);
        sqlSessionFactoryBean.setMapperLocations(resources);
        return sqlSessionFactoryBean.getObject();
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        logger.info("创建mapperScannerConfigurer成功");
        MapperScannerConfigurer mapperScannerConfigurer=new MapperScannerConfigurer();
        mapperScannerConfigurer.setAnnotationClass(Repository.class);
        mapperScannerConfigurer.setBasePackage("net.lainiao.dao");
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
        return mapperScannerConfigurer;
    }

    @Bean
    public DataSourceTransactionManager dataSourceTransactionManager(){
        DataSourceTransactionManager dataSourceTransactionManager=new DataSourceTransactionManager();
        DataSource dataSource= (DataSource) context.getBean("dataSource");
        dataSourceTransactionManager.setDataSource(dataSource);
        return dataSourceTransactionManager;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context=applicationContext;
    }
}
